package  com.storage.control
{
	import com.adobe.ac.mxeffects.CubeRotate;
	import com.adobe.ac.mxeffects.DistortionConstants;
	import com.adobe.ac.mxeffects.Push;
	import com.firemoss.modelglue.controller.ModelGlueController;
	import com.firemoss.modelglue.event.ModelGlueEvent;
	import com.storage.model.AppModelLocator;
	import com.storage.view.buy.BuyOrderList;
	import com.storage.view.buy.MerchandiseOfProvider;
	import com.storage.view.chart.AllBreakdown;
	import com.storage.view.chart.AllCategoryBreakdown;
	import com.storage.view.client.BuyerInfo;
	import com.storage.view.client.SupplierInfo;
	import com.storage.view.container.DisplayContainer;
	import com.storage.view.hr.AreaList;
	import com.storage.view.hr.DepartmentList;
	import com.storage.view.hr.EmployeeList;
	import com.storage.view.sell.SellOrderList;
	import com.storage.view.stock.WarehouseList;
	import com.storage.view.system.ChildModuleGrid;
	import com.storage.view.system.ModuleGrid;
	import com.storage.view.system.PermissionGrid;
	import com.storage.view.system.rolesGrid;
	import com.storage.view.system.usersGrid;
	import com.storage.view.warehouse.MerchandiseCategory;
	import com.storage.view.warehouse.MerchandiseList;
	import com.zzkj.view.widgets.InnerTab;
	
	import flexlib.containers.SuperTabNavigator;
	
	import mx.core.Container;
	import mx.events.EffectEvent;
	import mx.rpc.events.ResultEvent;
	
	public class MenuController extends ModelGlueController
	{
		[Bindable]
		public var model:AppModelLocator = AppModelLocator.getInstance();
		
		private var isPalying:Boolean = false;
		
		/***
		 * 单击主菜单事件
		 * */
		 
		private var container:DisplayContainer;
		private var nav:SuperTabNavigator;
		//2级菜单创建tab页
		public function menuItemClick(e:ModelGlueEvent):void{
			var item:XML = XML(e.getValue("item"));
			container = e.getValue('container') as DisplayContainer;
			nav = e.getValue('nav') as SuperTabNavigator;
			createComponentByMenu(item);

		}
		//根据xml的name属性来创建组建
		private function createComponentByMenu(item:XML):void{
			var name:String = item.@name;
			//先查找是否已经有这个组建
			var tab:InnerTab = model.displayPageDic[name] as  InnerTab;
			//如果tab为空，表示还没有这个tab，需要新建
			if(tab==null){
				tab = new InnerTab();
				//tab.parent.width = 100;
				tab.width = model.mainContainer.width - 10;
				tab.height = model.mainContainer.height - 10;
				//需要new的组建（页面）
				var cmp:Container ;
				//根据name判断需要new的组建cmp
				switch(name)
				{
					//申购单
					case 'AllBuyOrder':
						cmp = new BuyOrderList();
						break;
					case 'MyBuyOrder':
						cmp = new MerchandiseOfProvider();
						break;
					case 'ToGenerateBuyOrder':
						cmp = new MerchandiseList();
						break;
					//销售单
					case 'AllSellOrder':
						cmp = new SellOrderList();
						break;
					case 'MySellOrder':
						cmp = new SellOrderList();
						break;
					case 'ToGenerateSellOrder':
						cmp = new MerchandiseList();
						break;
					//仓库管理
					case 'MerchandiseCategory':
						cmp = new  MerchandiseCategory();
						break;
					case 'MerchandiseList':
						cmp = new MerchandiseList();
						break;
					case 'WarehouseList':
						cmp = new WarehouseList();
						break;
					case 'WarehouseList':
						cmp = new WarehouseList();
						break;
					//客户资料
					case 'PurchaserList':
						cmp = new  BuyerInfo();
						break;
					case 'ProviderList':
						cmp = new  SupplierInfo();
						break;
					//统计
					case 'SellStat':
						cmp = new AllCategoryBreakdown();
						break;
					case 'CapitalStat':
						cmp = new  AllBreakdown();
						break;
					//职工管理
					case 'EmployeeList':
						cmp = new EmployeeList();
						break;
					case 'DepartmentList':
						cmp = new DepartmentList();
						break;
					case 'AreaList':
						cmp = new AreaList();
						break;
					//系统 
					case 'UserList':
						cmp = new usersGrid();
						break;
					case 'RoleList':
						cmp = new  rolesGrid();
						break;
					case 'ModuleList':
						cmp = new ModuleGrid();
						break;
					case 'ChildModuleList':
						cmp = new ChildModuleGrid();
						break;
					case 'PermissionList':
						cmp = new PermissionGrid();
						break;
					//待审批的申请单 
					case 'BuyOrderForCheckup':
						cmp = new usersGrid();
						break;
					case 'SellOrderForCheckup':
						cmp = new  rolesGrid();
						break;
					case 'AdjustingWarehouseForCheckup':
						cmp = new WarehouseList();
						break;
					case 'PaymentNotice':
						cmp = new WarehouseList();
						break;
					case 'GatheringNotice':
						cmp = new WarehouseList();
						break;
					
				}
				//页面的style
				cmp.setStyle("paddingLeft",8);
				cmp.setStyle("paddingRight",8);
				cmp.setStyle("paddingBottom",2);
				cmp.setStyle("paddingTop",5);
				
				tab.addChild(cmp);
				tab.id = name;
				tab.current = cmp;
				model.displayPageDic[name] = tab;
			}
			//如果已经有的页面,在tab里面寻找cmp
			var index:Number = getChildIndexFormNav(tab);
			//index=-1表示没有
		 	if(index==-1){
		 		//把tab加进SuperTabNavigator
				nav.addChild(tab);
				//并选中当前添加的tab
				nav.selectedChild = tab;
			}else{
				//选中已有的tab
				nav.selectedIndex = index ;
			}	
			
			//设置tab的标题
			tab.label = item.@label;

		}
		
		//根据cmp（需要添加的自定义组件）去SuperTabNavigator的tab里查找是否已经存在
		private function getChildIndexFormNav(child:Container):Number{
			var ar:Array = nav.getChildren();
			if(ar.length==0){
				return -1;
			}
			for(var i:Number=0;i<ar.length;i++){
				var tp:Container = ar[i] as Container;
				if(child.id==tp.id){
					return i;
				}
			}
			return -1;
		}
		
		
		
		
		//事件2
		//播放效果
		public function playChangeEffect(e:ModelGlueEvent):void{
			var lab:String;
			if(e.exists('lab'))
				lab = e.getValue('lab');
			//效果开始播放的页面
			var b:Container = e.getValue('begin') as Container;
			//效果结束时，显示的页面
			var end:Container = e.getValue('end') as Container;
			//效果类型（）
			var type:String;
			if(e.exists('type'))
				type = e.getValue('type');
			//效果的方向
			var dir:String;
			if(e.exists('dir'))
				dir	= e.getValue('dir');
				
			playEffect(lab,b,end,type,dir);
		}

		public function playEffect(label:String,begin:Container,
							end:Container,type:String=null,direction:String=null):void{
			if(isPalying)
				return;
			var tab:InnerTab = begin.parent as InnerTab;
			var e:Object;
				switch(type)
				{
					//效果默认为push（可以改为defuatl）
					case null:
						e = new Push(begin);
						e.direction = DistortionConstants.LEFT;
						break;
					case 'CubeRotate':
						e = new CubeRotate(begin);
						if(direction==null)
							e.direction = DistortionConstants.LEFT;
						else
							e.direction = direction;
						break;
				}
			
			e.siblings = [ end ];
			e.duration = 800;
			e.addEventListener( EffectEvent.EFFECT_END, 
							function():void{
								//效果结束时删除当前的页面
								tab.removeChild( begin );
								//添加需要的页面
								tab.addChild(end );
								//tab的当前页面变更为end
								tab.current = end;
								//开始页面变更为tab的上一个页面
								tab.prev = begin;

								isPalying = false;
								
							} );
			isPalying = true;
			if(label!=null)
				tab.label = label;
			e.play();
		}
		
		
		
		
		
		
		
		
		
		
		
	
		/**
		 * 加载菜单事件
		 * *
		
		public function loadMenu(e:ModelGlueEvent):void{
			var httpService:HTTPService = new HTTPService();
				httpService.url = "assets/data/menu.xml";
				httpService.resultFormat = "e4x";
				httpService.addEventListener(FaultEvent.FAULT, onFaultHttpService);
				httpService.addEventListener(ResultEvent.RESULT, onResultHttpService);
				httpService.send();
		}
		private function onFaultHttpService(e:FaultEvent):void{
				Alert.show("Unable to load menu.xml.");
		}*/
		private function onResultHttpService(e:ResultEvent):void{
//			var xl:XMLList = e.result.menu;
//			//TODO
//			var nxm:XML = new XML('<menus></menus>');
//			for(var i:Number =0;i<xl.length();i++){
//				var xm:XML = xl[i] as XML;
//				var name:String = xm.@name;
//				if(name==="OrderManage"){
//					nxm.appendChild(xm);				
//				}else if(name=="buyIn"){
//					nxm.appendChild(xm);
//				}else if(name=="Production"){
//					nxm.appendChild(xm);
//				}
//				
//			}
//			model.appMenu = nxm.menu;
			//model.appMenu = e.result.menu;
		}


	}
}